var debug = true;
var $ = require("jquery");
require("jquery.cookie");
var dict = require('../model/dict.js');
var model = require('../model/question.js');
var view = require('../view/question.js');
var base = require("../base.js");

base.checklogin();


var cmd = parseInt($.cookie("cmd"));
var course = parseInt($.cookie("course"));
var sessionid = $.cookie("sessionid");
var queslist = [];

//初始化题目类型
dict.questype(function(list) {
	view.renderQuesType(list);
})

dict.courses(function(list){
	var coursename=list.filter(function(item,index) {
		return item.value==course;
	})[0].key;
	view.initBar(cmd,coursename);
})




/**
 * 查找题目 
 * @param  {[type]} id [description]
 * @return {[type]}    [description]
 */
function findById(id) {
	var s = queslist.filter(function(item, index) {
		return item.id == id;
	})[0];
	debug && console.log(s);
	return s;
}

//抽题的逻辑
$("#form-run").submit(function(event) {
	event.preventDefault();


	var amount = parseInt($('select[name="amount"]').val());
	var types=[];
	$('input[name="questype"]:checked').each(function(index, el) {
		types.push($(this).val());	
	});

	debug && console.log(types.join(","));
	switch (cmd) {
		case 1: //练习模式
			model.getPraQues(sessionid, amount, course, function(list) {
				if (list.status && list.status == 401) {
					location.href = "login.html";
				}
				queslist = list;
				view.renderQues(list);
				$('body').on('change', '.radio input', function(event) {
					event.preventDefault();
					var id = parseInt($(this).attr("name"));
					var selected = $(this).val();
					var tq = findById(id);
					if (selected.toUpperCase() != tq.answer.toUpperCase()) {
						$("#footer" + id).empty();
						$("#footer" + id).text('正确答案：' + tq.answer);
					} else {
						$("#footer" + id).empty();
					}
				});

			});
			break;
		case 2: //测试模式
			model.getTestQues(sessionid, amount, course, function(list) {
				if (list.status && list.status == 401) {
					location.href = "login.html";
				}
				queslist = list;
				view.renderQues(list);
				$('body').on('change', '.radio input', function(event) {
					event.preventDefault();
					var id = parseInt($(this).attr("name"));
					var selected = $(this).val();
					var tq = findById(id);
					$('input[name="' + id + '"]:not(:checked)').attr("disabled", true);
					if (selected.toUpperCase() != tq.answer.toUpperCase()) {
						$("#footer" + id).empty();
						$("#footer" + id).text('正确答案：' + tq.answer);
					}
				});
			});
			break;
		case 3: //错题模式
			model.getErrQues(sessionid, amount, course, function(list) {
				if (list.status && list.status == 401) {
					location.href = "login.html";
				}
				queslist = list;
				debug && console.log(list);
				view.renderQues(list);
				$('body').on('change', '.radio input', function(event) {
					event.preventDefault();
					var id = parseInt($(this).attr("name"));
					var selected = $(this).val();
					var tq = findById(id);
					if (selected.toUpperCase() != tq.answer.toUpperCase()) {
						$("#footer" + id).empty();
						$("#footer" + id).text('正确答案：' + tq.answer);
					} else {
						$("#footer" + id).empty();
					}
				});

			});
			break;

		default:
			debug && console.log("these is some error,cmd=" + cmd);
			break;
	}

});

//交卷的逻辑
$("#form-rate").submit(function(event) {
	/* Act on the event */
	event.preventDefault();
	var arr = $("#form-rate").serializeArray();
	var total = 0,
		right = [],
		done = [],
		error = [];
	for (var i = 0; i < arr.length; i++) {
		var id = parseInt(arr[i].name);
		var an = arr[i].value;
		if (an.toUpperCase() == findById(id).answer.toUpperCase()) {
			right.push(id);
			total++;
		} else {
			error.push(id);
		}
		done.push(id);
	}


	debug && console.log("have done:" + done.join(','));
	debug && console.log("error : " + error.join(','));

	

	var str = "";
	if (total < 60) {
		str = base.format("亲爱的，你的总分是：{}, 继续努力！", [total]);
	} else if (total < 70) {
		str = base.format("亲爱的，你的总分是：{}, 及格啦！", [total]);
	} else {
		str = base.format("亲爱的，你的总分是：{}, 太棒了!", [total]);
	}

	$("#result").text(str);
	$("#modal-result").modal('show');
	cmd == 1 && model.submitPra(sessionid, course, total, done.join(","), error.join(","), function(data) {
		if (data && data.status == 401) {
			location.href = "login.html";
		}
		debug && console.log(data);
	});
	cmd == 2 && model.submitTest(sessionid, course, total, done.join(","), error.join(","), function(data) {
		if (data.status && data.status == 401) {
			location.href = "login.html";
		}
		debug && console.log(data);
	});
	cmd == 3 && model.submitErr(sessionid, course, right.join(","), function(data) {
		if (data.status && data.status == 401) {
			location.href = "login.html";
		}
		debug && console.log(data);
	});
});